package com.sykj.cloud.system.controller;

import com.sykj.cloud.common.core.utils.poi.ExcelUtil;
import com.sykj.cloud.common.core.web.controller.BaseController;
import com.sykj.cloud.common.core.web.domain.AjaxResult;
import com.sykj.cloud.common.core.web.page.TableDataInfo;
import com.sykj.cloud.common.log.annotation.Log;
import com.sykj.cloud.common.log.enums.LogUserType;
import com.sykj.cloud.common.security.annotation.PreAuthorize;
import com.sykj.cloud.system.domain.SysUserParameter;
import com.sykj.cloud.system.service.ISysUserParameterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 用户参数 Controller
 *
 * @author mr.will
 * @date 2021-03-30
 */
@RestController
@RequestMapping("/user_parameter")
public class SysUserParameterController extends BaseController
{
    @Autowired
    private ISysUserParameterService sysUserParameterService;

    /**
     * 新增用户参数
     */
    @PreAuthorize(hasPermi = "system:user_parameter:add")
    @Log(title = "用户参数 ", logUserType = LogUserType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysUserParameter sysUserParameter)
    {
        int i=sysUserParameterService.insertSysUserParameter(sysUserParameter);
        if(i==-1){
            return AjaxResult.error("该用户参数code已经存在，不能再次新增");
        }
        return toAjax(i);
    }

    /**
     * 修改用户参数
     */
    @PreAuthorize(hasPermi = "system:user_parameter:edit")
    @Log(title = "用户参数 ", logUserType = LogUserType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysUserParameter sysUserParameter)
    {
        return toAjax(sysUserParameterService.updateSysUserParameter(sysUserParameter));
    }

    /**
     * 删除用户参数
     */
    @PreAuthorize(hasPermi = "system:user_parameter:remove")
    @Log(title = "用户参数 ", logUserType = LogUserType.DELETE)
    @DeleteMapping("/{guids}")
    public AjaxResult remove(@PathVariable String[] guids)
    {
        return toAjax(sysUserParameterService.deleteSysUserParameterByIds(guids));
    }

    /**
     * 获取用户参数 详细信息
     */
    @PreAuthorize(hasPermi = "system:user_parameter:query")
    @GetMapping(value = "/{guid}")
    public AjaxResult getInfo(@PathVariable("guid") String guid)
    {
        return AjaxResult.success(sysUserParameterService.getSysUserParameterById(guid));
    }

    /**
     * 导出用户参数 列表
     */
    @PreAuthorize(hasPermi = "system:user_parameter:export")
    @Log(title = "用户参数 ", logUserType = LogUserType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysUserParameter sysUserParameter) throws IOException
    {
        List<SysUserParameter> list = sysUserParameterService.listSysUserParameters(sysUserParameter);
        ExcelUtil<SysUserParameter> util = new ExcelUtil<SysUserParameter>(SysUserParameter.class);
        util.exportExcel(response, list, "user_parameter");
    }

    /**
     * 查询用户参数 列表
     */
    @PreAuthorize(hasPermi = "system:user_parameter:list")
    @GetMapping("/list")
    public TableDataInfo list(SysUserParameter sysUserParameter)
    {
        startPage();
        List<SysUserParameter> list = sysUserParameterService.listSysUserParameters(sysUserParameter);
        return getDataTable(list);
    }
}
